using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
using BUS;
using DAL;
using Utility;

public partial class Admin_KhachHangList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
        if (Request.Url.ToString().Contains("dangnhap.aspx") == false && Session["tk"] == null)
            Response.Redirect("~/Admin/dangnhap.aspx");

    }
    private void LoadData()
    {
        Pager.PageSize = 5;
        Criteria c = new Criteria();
        if (txt_id_kh.Text != "")
        {
            c.add(Condition.AND, tbKhachHang.tbKhachHangColumns .IDKhachHang, Condition.LIKE,
                txt_id_kh.Text);
        }
        if (txt_tkh.Text != "")
        {
            c.add(Condition.AND, tbKhachHang .tbKhachHangColumns .TenKH , Condition.LIKE, txt_tkh.Text);
        }
        int total = 0;
        tbKhachHang .tbKhachHangCollection  kh_Collection =
            tbKhachHangBUS .Get(c.Criter, Pager.CurrentIndex, Pager.PageSize,
           tbKhachHang .tbKhachHangColumns .IDKhachHang.ToString(), OrderDirection.ASC.ToString(), out total);
        Repeater1.DataSource = kh_Collection;
        Repeater1.DataBind();
        Pager.ItemCount = total;
        if (total <= Pager.PageSize)
            Pager.Visible = false;
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
            e.Item.ItemType == ListItemType.Item)
        {
            tbKhachHang kh = (tbKhachHang)e.Item.DataItem;
            HyperLink hyp_tkh = (HyperLink)e.Item.FindControl("hyp_tkh");


            Literal lit_Ma = (Literal)e.Item.FindControl("lit_Ma");
            HiddenField hf_id = (HiddenField)e.Item.FindControl("hf_id");


            lit_Ma.Text = kh.IDKhachHang.ToString();
            hf_id.Value = lit_Ma.Text;

            hyp_tkh.Text = kh.TenKH.ToString();
 
            hyp_tkh.NavigateUrl = "KhachHangCT.aspx?id=" + kh.IDKhachHang.ToString(); ;


            HyperLink hpl_Edit = (HyperLink)e.Item.FindControl("hpl_Edit");
            hpl_Edit.NavigateUrl = "KhachHangUpdate.aspx?id=" + kh.IDKhachHang.ToString();


        }
    }
    protected void Pager_Command(object sender, CommandEventArgs e)
    {
        Pager.CurrentIndex = int.Parse(e.CommandArgument.ToString());
        LoadData();
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        LoadData();

    }
    protected void lbt_Xoa_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < Repeater1.Items.Count; i++)
        {
            CheckBox cb_Xoa = (CheckBox)Repeater1.Items[i].FindControl("cb_Xoa");
            if (cb_Xoa.Checked == true)
            {
                HiddenField hf_id = (HiddenField)Repeater1.Items[i].FindControl("hf_id");
                int id = int.Parse(hf_id.Value);
                tbKhachHang  kh = tbKhachHangBUS .GetByID(id);
                tbKhachHangBUS.Delete (id);
            }
        }
        Response.Redirect(Request.RawUrl);

    }
}
